package com.zhonghui.modlules.user.eshopService.Impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.apiCode.EshopException;
import com.eshop.domain.BaseDomain;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDomain.ShopUser;
import com.zhonghui.common.eshopDozer.config.IGenerator;
import com.zhonghui.common.eshopEnum.BillDetailEnum;
import com.zhonghui.modlules.shop.eshopService.SystemGroupDataService;
import com.zhonghui.modlules.user.eshopDomain.UserBill;
import com.zhonghui.modlules.user.eshopDomain.UserSign;
import com.zhonghui.modlules.user.eshopMapper.UserBillMapper;
import com.zhonghui.modlules.user.eshopMapper.UserSignMapper;
import com.zhonghui.modlules.user.eshopService.UserBillService;
import com.zhonghui.modlules.user.eshopService.UserService;
import com.zhonghui.modlules.user.eshopService.UserSignService;
import com.zhonghui.modlules.user.eshopVo.SignVo;
import com.zhonghui.modlules.user.eshopVo.UserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
@Transactional(
        rollbackFor = {Exception.class}
)
public class UserSignServiceImpl extends BaseServiceImpl<UserSignMapper, UserSign> implements UserSignService {
    @Autowired
    private SystemGroupDataService systemGroupDataService;
    @Autowired
    private UserSignMapper userSignMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserBillMapper userBillMapper;
    @Autowired
    private UserBillService billService;
    @Autowired
    private IGenerator generator;
    public int sign(ShopUser shopUser) {
        List<JSONObject> list = this.systemGroupDataService.getDatas("yshop_sign_day_num");
        if (!ObjectUtil.isNull(list) && !list.isEmpty()) {
            boolean isDaySign = this.getToDayIsSign(shopUser.getUid());
            if (isDaySign) {
                throw new EshopException("已签到");
            } else {
                int signNumber = 0;
                int userSignNum = shopUser.getSignNum();
                if (this.getYesterDayIsSign(shopUser.getUid())) {
                    if (shopUser.getSignNum() > list.size() - 1) {
                        userSignNum = 0;
                    }
                } else {
                    userSignNum = 0;
                }

                int index = 0;

                for(Iterator var7 = list.iterator(); var7.hasNext(); ++index) {
                    Map<String, Object> map = (Map)var7.next();
                    if (index == userSignNum) {
                        signNumber = Integer.valueOf(map.get("sign_num").toString());
                        break;
                    }
                }

                ++userSignNum;
                UserSign userSign = new UserSign();
                userSign.setUid(shopUser.getUid());
                String title = "签到奖励";
                if (userSignNum == list.size()) {
                    title = "连续签到奖励";
                }

                userSign.setTitle(title);
                userSign.setNumber(signNumber);
                userSign.setBalance(shopUser.getIntegral().intValue());
                this.userSignMapper.insert(userSign);
                ShopUser user = ShopUser.builder().integral( NumberUtil.add(shopUser.getIntegral(), signNumber)).uid(shopUser.getUid()).signNum(userSignNum).build();
                boolean res = this.userService.updateById(user);
                if (!res) {
                    throw new EshopException("签到失败");
                } else {
                    this.billService.income(shopUser.getUid(), title, BillDetailEnum.CATEGORY_2.getValue(), BillDetailEnum.TYPE_10.getValue(), (double)signNumber, shopUser.getIntegral().doubleValue(), "", "");
                    return signNumber;
                }
            }
        } else {
            throw new EshopException("请先配置签到天数");
        }
    }

    private boolean getToDayIsSign(Long uid) {
        Date today = DateUtil.beginOfDay(new Date());
        int count = this.lambdaQuery().eq(UserSign::getUid, uid).ge( BaseDomain::getCreateTime, today).count();
        return count > 0;
    }
    private boolean getYesterDayIsSign(Long uid) {
        Date today = DateUtil.beginOfDay(new Date());
        Date yesterday = DateUtil.beginOfDay(DateUtil.yesterday());
        int count = this.lambdaQuery().eq(UserSign::getUid, uid).lt(BaseDomain::getCreateTime, today).ge(BaseDomain::getCreateTime, yesterday).count();
        return count > 0;
    }

    public List<SignVo> getSignList(Long uid, int page, int limit) {
        Page<UserBill> pageModel = new Page((long)page, (long)limit);
        return this.userBillMapper.getSignList(uid, pageModel);
    }

    public UserQueryVo userSignInfo(ShopUser shopUser) {
        UserQueryVo userQueryVo = (UserQueryVo)this.generator.convert(shopUser, UserQueryVo.class);
        Long uid = shopUser.getUid();
        int sumSignDay = this.getSignSumDay(uid);
        boolean isDaySign = this.getToDayIsSign(uid);
        boolean isYesterDaySign = this.getYesterDayIsSign(uid);
        userQueryVo.setSumSignDay(sumSignDay);
        userQueryVo.setIsDaySign(isDaySign);
        userQueryVo.setIsYesterDaySign(isYesterDaySign);
        if (!isDaySign && !isYesterDaySign) {
            userQueryVo.setSignNum(0);
        }

        return userQueryVo;
    }

    private int getSignSumDay(Long uid) {
        return ((LambdaQueryChainWrapper)this.lambdaQuery().eq(UserSign::getUid, uid)).count();
    }
}
